﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Cat.Helper;
using System.IO;
using System.Text.RegularExpressions;

namespace PicServer.Bcy
{
    class Bcy_start
    {
        public void scanner_start()
        {
            initdata();
            list_reader(string.Format(Bcy_static_value.index_url, DateTime.Now.ToString("yyyymmdd")));
        }

        int pagecount = 0;
        Queue<string> old_url = new Queue<string>();
        private void initdata()
        {
            new File_helper().File_existence(Bcy_static_value.bcy_temp);

        }
        private void list_reader(string url)
        {
            Console.WriteLine(url);
            if (pagecount < 1)
            {
                new Web_helper().HttpGetAsync(url, List_complete);
            }
            else
            {
                Console.WriteLine("全部读取完成");

                Single_content_download();
            }
        }



        private void List_complete(object sender, OpenReadCompletedEventArgs e)
        {
            string shtml = Html_helper.get_html(e);


            foreach (String line in new Html_helper().Get_href(shtml, "//div[@class='work-thumbnail__topBd']/a"))
            {
                Console.WriteLine(line);
                if (!Bcy_static_value.unpublish.Contains(line) && !Bcy_static_value.Bcy_old_url.Contains(line))
                {
                    Bcy_static_value.unpublish.Add(line);
                    File.AppendAllText(Bcy_static_value.bcy_temp + "pageurl.txt", "\r\n" + line);
                }
                else
                {
                    Console.WriteLine("已添加过");

                }

            }

            //     old_url.Enqueue(new Html_helper().Get_href(shtml, "//div[@class='dateCalendar']/a[1]")[0]);
            pagecount++;//循环读取近期列表
            list_reader("https://bcy.net/coser/" + new Html_helper().Get_href(shtml, "//div[@class='dateCalendar']/a[1]")[0]);//启动下一次列表读取

        }
        /// <summary>
        /// 唤起单页下载
        /// </summary>
        private void Single_content_download()
        {
            Bcy_static_value.exclude_key = new List_helper().get_text_value(Bcy_static_value.bcy_temp, "exclude_key.txt");
            if (Bcy_static_value.unpublish.Count > 0)
            {
                string url = "https://bcy.net" + Bcy_static_value.unpublish[0];
                Bcy_static_value.unpublish.RemoveAt(0);//读取一个画师并且从列表删除

                File.WriteAllLines(Bcy_static_value.bcy_temp + "pageurl.txt", Bcy_static_value.unpublish);

                Console.WriteLine("当前扫描：" + url);
                try
                {
                    if (!Bcy_static_value.Bcy_old_url.Contains(url))
                    {

                        new Web_helper().HttpGetAsync(url, Content_complete);
                        Bcy_static_value.Bcy_old_url.Add(url);
                        File.AppendAllText(Bcy_static_value.bcy_temp + "old_url.txt", "\r\n" + url.Replace("https://bcy.net", ""));
                    }
                    else
                    {
                        Console.WriteLine(" 已扫描过，下一个" + Bcy_static_value.Bcy_old_url.Contains(url));
                        Single_content_download();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("网络异常情况+" + ex);
                }

            }
        }

        private void Content_complete(object sender, OpenReadCompletedEventArgs e)
        {
            string shtml = Html_helper.get_html(e);
            if (new Html_helper().Get_text(shtml, "//i[@class='i-work-sex-0 posa user-sex']").Count > 0)
            {
                Console.WriteLine("女性coser，可以扫描！");

            }
            else
            {
                Console.WriteLine("男性coser，放弃扫描！");
                //Single_content_download();
                //  return;
            }
            Queue<string> downloadList = new Queue<string>();
            string title = "", child_title = "";

            try
            {

                title = new Html_helper().Get_text(shtml, "//title")[0].Replace(" | 半次元-第一中文COS绘画小说社区", "");
                Console.WriteLine("-----------" + title);
                List<string> page_title = new Html_helper().Get_text(shtml, "//div[@class='post__title']/h1");
                if (page_title.Count > 0)//获取页面的子标题
                    child_title = page_title[0];
                if (title.Trim() != child_title.Trim())
                    title = title + child_title;
                if (title.Length > 100)//裁剪贼长的标题
                    title = title.Substring(0, 60);
            }
            catch (Exception ex)
            {
                Console.WriteLine("标题为空！" + ex);
            }
            string content = "";
            List<string> type_info = new Html_helper().Get_text(shtml, "//div[@class='post__content type-info']");
            List<string> ph30 = new Html_helper().Get_text(shtml, "//div[@class='l-clearfix ph30']");

            if (type_info.Count > 0 && ph30.Count > 0)//这个部分是采集文章内容
                content = type_info[0] + ph30[0];
            else if (type_info.Count > 0)
                content = type_info[0];
            else if (ph30.Count > 0)
                content = ph30[0];

            string tag = "";
            foreach (String line in new Html_helper().Get_src(shtml, "//img[@class='detail_std detail_clickable']"))
            {
                int sindex = line.Length - 10;


                string url_temp = line.Substring(sindex).Replace("/w650", "");

                string img_url = "";


                if (!Bcy_static_value.test_mode)//为非时解除测试模式，现在已经解除
                {
                    img_url = line;//测试模式下不下载大图，这样减少硬盘IO 
                }
                else
                {

                    if (Path.GetExtension(url_temp) == ".jpg" || Path.GetExtension(url_temp) == ".png")//前真后不做，能省一次运算
                        img_url = line.Substring(0, sindex) + url_temp;
                    else
                        img_url = line.Substring(0, sindex) + url_temp + ".jpg";//处理无文件后缀的情况
                }
                Console.WriteLine(img_url);
                downloadList.Enqueue(img_url);
            }

            foreach (String line in new Html_helper().Get_text(shtml, "//div[@class='post__detail-tag']/span/ul/li"))
            {
                tag += string.Format(",{0}", line.Trim());

            }
            tag = Regex.Replace(tag, @"\s+", " ").Trim();
            content = Regex.Replace(content, @"\s+", " ").Trim();

            if (new List_helper().exclude_content(new List<string>() { title, tag }, Bcy_static_value.exclude_key) && downloadList.Count > 0)

                start_download(title, downloadList, content, tag);
            else
            {
                Console.WriteLine("找到需要屏蔽关键词！或者下载数量太小：{0}", downloadList.Count);
                Single_content_download();
            }

        }

        private void start_download(string title, Queue<string> downloadList, string content, string tag)
        {
            string download_path = Bcy_static_value.bcy_download + new File_helper().ReplaceBadChar(title) + "DTime" + DateTime.Now.ToFileTime().ToString() + "\\";
            new File_helper().File_existence(download_path);
            File.AppendAllText(download_path + "content.txt", content);
            File.AppendAllText(download_path + "tags.txt", tag);
            int count = 0;
            while (downloadList.Count > 0)
            {
                Console.WriteLine("剩余待下载{0}", downloadList.Count);
                string img_url = downloadList.Dequeue();
                using (WebClient mywebclient = new WebClient())
                {
                    try
                    {
                        mywebclient.Headers.Add("Host", new Uri(img_url).Host);
                        mywebclient.Headers.Add("UserAgent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0");

                        if (Bcy_static_value.test_mode)//测试模式，下载小图
                            mywebclient.DownloadFile(img_url, download_path + DateTime.Now.ToFileTime().ToString() + ".jpg");
                        else
                            mywebclient.DownloadFile(img_url, download_path + Path.GetFileName(img_url));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);

                    }
                }
            }
            Single_content_download(); //启动继续下载
        }


    }
}
